//
//  DestinationsResultPushViewController.m
//  TripGo
//
//  Created by dllo on 15/7/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "DestinationsResultPushViewController.h"
#import "DestinationsResultPushTableViewCell.h"
#import "AFNetworkHandler.h"
#import "AttractionTripTags.h"
#import "AttractionContents.h"
#import "Trip.h"
#import "Notes.h"
#import "DestinationDetail.h"
#import "UIImageView+WebCache.h"
@interface DestinationsResultPushViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) UIImageView *headerView;
@property (nonatomic, retain) NSMutableArray *arrW;
@property (nonatomic, retain) NSMutableArray *arr;
@property (nonatomic, retain) UIActivityIndicatorView *activityIndicator;
@end

@implementation DestinationsResultPushViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
    [self getData];
    
    // Do any additional setup after loading the view.
}
- (void)createTableView
{
    // 定义菊花控件
    self.activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:self.activityIndicator];
    self.activityIndicator.color = [UIColor grayColor];
    self.activityIndicator.center = self.view.center;
    [self.activityIndicator startAnimating];
    [_activityIndicator release];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [_tableView release];
    self.headerView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)];
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 70, 0);
    self.tableView.tableHeaderView = self.headerView;
    [_headerView release];
}
- (void)getData
{
    NSString *str = [NSString stringWithFormat:@"http://chanyouji.com/api/attractions/%@.json?page=1", self.passId];
    [AFNetworkHandler GetWithAFNByURL:str completion:^(id result) {
        NSDictionary *dic = result;
        self.arrW = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        DestinationDetail *des = [[DestinationDetail alloc] initWithDic:dic];
        [self.arrW addObject:des];
        [self.headerView setImageWithURL:[NSURL URLWithString:des.image_url] placeholderImage:[UIImage imageNamed:@"place_holder.jpg"]];
        NSArray *arr = des.attraction_trip_tags;
        for (NSDictionary *dic in arr) {
            AttractionTripTags *attraction = [[AttractionTripTags alloc] initWithDic:dic];
            NSArray *arr = attraction.attraction_contents;
            for (NSDictionary *dic in arr) {
                AttractionContents *att = [[AttractionContents alloc] initWithDic:dic];
                NSDictionary *tripDic = att.attraction_contents_trip;
                Trip *t = [[Trip alloc] initWithDic:tripDic];
                NSMutableArray *arr = [NSMutableArray array];
                [arr addObject:t];
                NSArray *notesArr = att.attraction_contents_notes;
                for (NSDictionary *dic in notesArr) {
                    Notes *n = [[Notes alloc] initWithDic:dic];
                    NSMutableArray *arr = [NSMutableArray array];
                    [arr addObject:n];
                }
            }
            [self.arr addObject:attraction];
        }
        [self.tableView reloadData];
        [self.activityIndicator stopAnimating];
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.arr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
//    NSDictionary *dic = self.arr[section];
//    NSArray *arr = [NSArray array];
//    AttractionTripTags *t = [[AttractionTripTags alloc] initWithDic:dic];
//    arr = t.attraction_contents;
    return [[self.arr[section] attraction_contents] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //NSInteger *row = [indexPath row];
    DestinationsResultPushTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tCell"];
    if (!cell) {
        cell = [[DestinationsResultPushTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"tCell"];
    }
//    NSDictionary *dic = self.arr[indexPath.section];
//    AttractionTripTags *t = [[AttractionTripTags alloc] initWithDic:dic];
//    NSArray *arr = t.attraction_contents;
//    cell.desDetail = arr[indexPath.row];
    NSDictionary *dic = [self.arr[indexPath.section] attraction_contents][indexPath.row];
    AttractionContents *t = [[AttractionContents alloc] initWithDic:dic];
    cell.att = t;
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return [self.arr[section] name];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.arr[indexPath.section] attraction_contents][indexPath.row];
    AttractionContents *t = [[AttractionContents alloc] initWithDic:dic];
    CGFloat h1 = [DestinationsResultPushTableViewCell heightWithText:t.attraction_contents_description];
    CGFloat h2 = 150;
    CGFloat h3 = 25;
    return h1 + h2 + h3 + 10;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
